using System;
using System.Data;
using System.Data.SqlClient;
using Entity;

namespace SalesVMT
{
    public class CustomerDB
    {
        #region PUBLIC
        public static DataSet GetAll()
        {
            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_Customer_GetAll", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetMoneySend(int CustomerID)
        {
            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_Customer_GetMoneySend", dbConn);
            dbCmd.Parameters.AddWithValue("@Customer_ID", CustomerID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetAllFromShipment()
        {
            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_Customer_GetAllFromShipment", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetAllFromShipment1()
        {
            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_Customer_GetAllFromShipment1", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static DataSet GetList(out int ItemCount, int customertype_ID, string keyWord, int currentPage, int pageSize)
        {

            DataSet retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Customer_GetList", dbConn);
            dbCmd.Parameters.AddWithValue("@Customer_CustomerTypeID", customertype_ID);
            dbCmd.Parameters.AddWithValue("@KeyWord", keyWord);
            dbCmd.Parameters.AddWithValue("@CurrentPage", currentPage);
            dbCmd.Parameters.AddWithValue("@RPP", pageSize);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                retVal = new DataSet();
                SqlDataAdapter da = new SqlDataAdapter(dbCmd);
                da.Fill(retVal);
                ItemCount = Convert.ToInt32(retVal.Tables[1].Rows[0][0]);
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;

        }
        public static bool Delete(int _customer_ID)
        {
            bool retVal = false;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Customer_Delete", dbConn);
            dbCmd.Parameters.AddWithValue("@Customer_ID", _customer_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static int Insert(CustomerInfo _CustomerInfo)
        {
            int retVal = 0;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Customer_Insert", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Customer_CustomerTypeID", _CustomerInfo.Customer_CustomerTypeID);
            dbCmd.Parameters.AddWithValue("@Customer_Code", _CustomerInfo.Customer_Code);
            dbCmd.Parameters.AddWithValue("@Customer_Name", _CustomerInfo.Customer_Name);
            dbCmd.Parameters.AddWithValue("@Customer_Address", _CustomerInfo.Customer_Address);
            dbCmd.Parameters.AddWithValue("@Customer_Phone", _CustomerInfo.Customer_Phone);
            dbCmd.Parameters.AddWithValue("@Customer_Fax", _CustomerInfo.Customer_Fax);
            dbCmd.Parameters.AddWithValue("@Customer_TaxNo", _CustomerInfo.Customer_TaxNo);
            dbCmd.Parameters.AddWithValue("@Customer_Contact", _CustomerInfo.Customer_Contact);
            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static bool Update(CustomerInfo _CustomerInfo)
        {
            bool retVal = false;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Customer_Update", dbConn);
            
            dbCmd.Parameters.AddWithValue("@Customer_ID", _CustomerInfo.Customer_ID);
            dbCmd.Parameters.AddWithValue("@Customer_CustomerTypeID", _CustomerInfo.Customer_CustomerTypeID);
            dbCmd.Parameters.AddWithValue("@Customer_Code", _CustomerInfo.Customer_Code);
            dbCmd.Parameters.AddWithValue("@Customer_Name", _CustomerInfo.Customer_Name);
            dbCmd.Parameters.AddWithValue("@Customer_Address", _CustomerInfo.Customer_Address);
            dbCmd.Parameters.AddWithValue("@Customer_Phone", _CustomerInfo.Customer_Phone);
            dbCmd.Parameters.AddWithValue("@Customer_Fax", _CustomerInfo.Customer_Fax);
            dbCmd.Parameters.AddWithValue("@Customer_TaxNo", _CustomerInfo.Customer_TaxNo);
            dbCmd.Parameters.AddWithValue("@Customer_Contact", _CustomerInfo.Customer_Contact);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                if (dbCmd.ExecuteNonQuery() > 0)
                {
                    retVal = true;
                }
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static CustomerInfo GetInfo(int _customer_ID)
        {
            CustomerInfo retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Customer_GetInfo", dbConn);
            dbCmd.Parameters.AddWithValue("@Customer_ID", _customer_ID);
            dbCmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader dr = null;

            try
            {
                dbConn.Open();
                dr = dbCmd.ExecuteReader();
                if (dr.Read())
                {
                    retVal = new CustomerInfo();
                    retVal.Customer_ID = Convert.ToInt32(dr["Customer_ID"]);
                    retVal.Customer_CustomerTypeID = Convert.ToInt32(dr["Customer_CustomerTypeID"]);
                    retVal.Customer_Code = Convert.ToString(dr["Customer_Code"]);
                    retVal.Customer_Name = Convert.ToString(dr["Customer_Name"]);
                    retVal.Customer_Address = Convert.ToString(dr["Customer_Address"]);
                    retVal.Customer_Phone = Convert.ToString(dr["Customer_Phone"]);
                    retVal.Customer_Fax = Convert.ToString(dr["Customer_Fax"]);
                    retVal.Customer_TaxNo = Convert.ToString(dr["Customer_TaxNo"]);
                    retVal.Customer_Contact = Convert.ToString(dr["Customer_Contact"]);
                    if (dr["Customer_MoneySend"] != DBNull.Value)
                        retVal.Customer_MoneySend = Convert.ToDecimal(dr["Customer_MoneySend"]);
                    else
                        retVal.Customer_MoneySend = 0;
                }
            }
            finally
            {
                if (dr != null) dr.Close();
                dbConn.Close();
            }
            return retVal;
        }
        public static string GetMaxCode()
        {
            string retVal = null;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_Customer_GetMaxCode", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            try
            {
                dbConn.Open();
                retVal = Convert.ToString(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static int Using(int CustomerID)
        {
            int retVal = 0;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();
            SqlCommand dbCmd = new SqlCommand("SalesVMT_Customer_Using", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Customer_ID", CustomerID);

            try
            {
                dbConn.Open();
                retVal = Convert.ToInt32(dbCmd.ExecuteScalar());
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        public static bool Exist(int CustomerID, string CustomerName)
        {
            bool retVal = false;
            SqlConnection dbConn = SQLServerUtil.CreateConnection();

            SqlCommand dbCmd = new SqlCommand("SalesVMT_Customer_Exist", dbConn);
            dbCmd.CommandType = CommandType.StoredProcedure;
            dbCmd.Parameters.AddWithValue("@Customer_Name", CustomerName);
            dbCmd.Parameters.AddWithValue("@Customer_ID", CustomerID);
            try
            {
                dbConn.Open();
                if (Convert.ToInt32(dbCmd.ExecuteScalar()) > 0)

                    retVal = true;
            }
            finally
            {
                dbConn.Close();
            }
            return retVal;
        }
        #endregion
    }
}
